﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKRevitStairs.StairsEdit.Common;

namespace YJKRevitStairs.StairsEdit.Managers
{
	internal class JoinOnePairStairsCls
	{
		public JoinOnePairStairsCls(ExternalCommandData commandData, Stairs stairs1, Stairs stairs2)
		{
			this.m_commandData = commandData;
			this.m_doc = this.m_commandData.Application.ActiveUIDocument.Document;
			this.m_stair1 = stairs1;
			this.m_stair2 = stairs2;
		}

		public bool JoinOnePairStairsFun()
		{
			return this.CheckConflict() && this.Merge2Stairs();
		}

		private bool CheckConflict()
		{
			if (!this.m_stair1.ActualRiserHeight.Equals(this.m_stair2.ActualRiserHeight))
			{
				return false;
			}
			if (!this.m_stair1.ActualTreadDepth.Equals(this.m_stair2.ActualTreadDepth))
			{
				return false;
			}
			if (this.m_stair1.ActualRisersNumber != this.m_stair2.ActualRisersNumber)
			{
				return false;
			}
			if (this.m_stair1.ActualTreadsNumber != this.m_stair2.ActualTreadsNumber)
			{
				return false;
			}
			ElementId elementId = this.m_stair1.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
			ElementId elementId2 = this.m_stair1.GetParameter(BuiltInParameter.STAIRS_TOP_LEVEL_PARAM).AsElementId();
			ElementId elementId3 = this.m_stair1.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
			ElementId elementId4 = this.m_stair1.GetParameter(BuiltInParameter.STAIRS_TOP_LEVEL_PARAM).AsElementId();
			return !(elementId != elementId3) && !(elementId2 != elementId4);
		}

		private bool Merge2Stairs()
		{
			Transaction transaction = new Transaction(this.m_doc, "合并两个楼梯");
			transaction.Start();
			try
			{
				StairsType stairsType = this.m_doc.GetElement(this.m_stair1.GetTypeId()) as StairsType;
				StairsRunType stairsRunType = this.m_doc.GetElement(stairsType.RunType) as StairsRunType;
				StairsLandingType stairsLandingType = this.m_doc.GetElement(stairsType.LandingType) as StairsLandingType;
				StairsType stairsType2 = this.m_doc.GetElement(this.m_stair2.GetTypeId()) as StairsType;
				StairsRunType stairsRunType2 = this.m_doc.GetElement(stairsType2.RunType) as StairsRunType;
				StairsLandingType stairsLandingType2 = this.m_doc.GetElement(stairsType2.LandingType) as StairsLandingType;
				string newStairsTypeName = this.GetNewStairsTypeName(stairsType.Name);
				StairsType stairsType3 = stairsType.Duplicate(newStairsTypeName) as StairsType;
				stairsType3.HasMiddleSupports = stairsType2.HasMiddleSupports;
				if (stairsType3.HasMiddleSupports)
				{
					stairsType3.MiddleSupportsNumber = stairsType2.MiddleSupportsNumber;
					stairsType3.MiddleSupportType = stairsType2.MiddleSupportType;
				}
				int num = stairsType2.GetParameter(BuiltInParameter.STAIRSTYPE_HAS_LEFT_SUPPORT).AsInteger();
				stairsType3.GetParameter(BuiltInParameter.STAIRSTYPE_HAS_LEFT_SUPPORT).Set(num);
				if (num == 2)
				{
					stairsType3.LeftSideSupportType = stairsType2.LeftSideSupportType;
					stairsType3.LeftLateralOffset = stairsType2.LeftLateralOffset;
				}
				num = stairsType2.GetParameter(BuiltInParameter.STAIRSTYPE_HAS_RIGHT_SUPPORT).AsInteger();
				stairsType3.GetParameter(BuiltInParameter.STAIRSTYPE_HAS_RIGHT_SUPPORT).Set(num);
				if (num == 2)
				{
					stairsType3.RightSideSupportType = stairsType2.RightSideSupportType;
					stairsType3.RightLateralOffset = stairsType2.RightLateralOffset;
				}
				double riserThickness = stairsRunType.RiserThickness;
				double treadThickness = stairsRunType.TreadThickness;
				string text = (stairsRunType2.StructuralDepth * CommonUnit.MM2Feet).ToString("f1");
				text += "mm";
				text += "楼梯板厚 ";
				text += (riserThickness * CommonUnit.MM2Feet).ToString("f1");
				text += "mm";
				text += "踢面厚 ";
				text += (riserThickness * CommonUnit.MM2Feet).ToString("f1");
				text += "mm";
				text += "踏板厚";
				StairsRunType stairsRunType3 = null;
				bool flag = false;
				CommonUnit.GetElementType<StairsRunType>(this.m_doc, text, ref stairsRunType3, ref flag);
				if (flag)
				{
					stairsRunType3 = (stairsRunType.Duplicate(text) as StairsRunType);
					stairsRunType3.StructuralDepth = stairsRunType2.StructuralDepth;
					stairsRunType3.UndersideSurfaceStyle = stairsRunType2.UndersideSurfaceStyle;
				}
				Parameter parameter = stairsRunType3.GetParameter(BuiltInParameter.STAIRS_RUNTYPE_RUN_MATERIAL);
				if (parameter != null && !parameter.IsReadOnly)
				{
					Parameter parameter2 = stairsRunType2.GetParameter(BuiltInParameter.STAIRS_RUNTYPE_RUN_MATERIAL);
					if (parameter2 != null)
					{
						ElementId elementId = parameter2.AsElementId();
						if (elementId != null && elementId.IntegerValue != -1)
						{
							parameter.Set(elementId);
						}
					}
				}
				stairsType3.RunType = stairsRunType3.Id;
				double thickness = stairsLandingType2.Thickness;
				string text2 = (thickness * CommonUnit.MM2Feet).ToString("f1") + "mm休息板厚";
				if (stairsLandingType.GetParameter(BuiltInParameter.STAIRS_LANDINGTYPE_USE_SAME_TRISER_AS_RUN).AsInteger() == 0)
				{
					int num2 = stairsLandingType.GetParameter(BuiltInParameter.STAIRS_TRISERTYPE_TREAD).AsInteger();
					if (1 == num2)
					{
						double num3 = stairsLandingType.GetParameter(BuiltInParameter.STAIRS_TRISERTYPE_TREAD_THICKNESS).AsDouble();
						text2 += " ";
						text2 += (num3 * CommonUnit.MM2Feet).ToString("f1");
						text2 += "mm踏板厚";
					}
				}
				StairsLandingType stairsLandingType3 = null;
				flag = true;
				CommonUnit.GetElementType<StairsLandingType>(this.m_doc, text2, ref stairsLandingType3, ref flag);
				if (flag)
				{
					stairsLandingType3 = (stairsLandingType.Duplicate(text2) as StairsLandingType);
					stairsLandingType3.Thickness = thickness;
				}
				Parameter parameter3 = stairsLandingType3.GetParameter(BuiltInParameter.STAIRS_LANDINGTYPE_LANDING_MATERIAL);
				if (parameter3 != null && !parameter3.IsReadOnly)
				{
					Parameter parameter4 = stairsLandingType2.GetParameter(BuiltInParameter.STAIRS_LANDINGTYPE_LANDING_MATERIAL);
					if (parameter4 != null)
					{
						ElementId elementId2 = parameter4.AsElementId();
						if (elementId2 != null && elementId2.IntegerValue != -1)
						{
							parameter3.Set(elementId2);
						}
					}
				}
				stairsType3.LandingType = stairsLandingType3.Id;
				List<ElementId> list = new List<ElementId>();
				try
				{
					list = (this.m_doc.GetElement(this.m_stair1.GroupId) as Group).UngroupMembers().ToList<ElementId>();
				}
				catch (Exception)
				{
				}
				this.m_stair1.ChangeTypeId(stairsType3.Id);
				try
				{
					ElementSet elementSet = new ElementSet();
					foreach (ElementId elementId3 in list)
					{
						elementSet.Insert(this.m_doc.GetElement(elementId3));
					}
					GroupType groupType = this.m_doc.CreatYJKGroup(elementSet).GroupType;
					if (groupType.CanBeRenamed && this.m_stair1.GetTypeId() != null)
					{
						StairsType stairsType4 = this.m_doc.GetElement(this.m_stair1.GetTypeId()) as StairsType;
						if (stairsType4 != null)
						{
							groupType.Name = stairsType4.Name;
						}
					}
				}
				catch (Exception)
				{
				}
				this.m_doc.Delete(this.m_stair2.Id);
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				return false;
			}
			return true;
		}

		private string GetNewStairsTypeName(string strName)
		{
			List<int> list = new List<int>();
			foreach (Element element in new FilteredElementCollector(this.m_doc).OfClass(typeof(StairsType)))
			{
				StairsType stairsType = element as StairsType;
				if (stairsType != null)
				{
					string name = stairsType.Name;
					if (name.Contains(strName))
					{
						int num = name.LastIndexOf("-");
						if (num != -1 && name.Substring(0, num).CompareTo(strName) == 0)
						{
							string value = name.Substring(num + 1);
							try
							{
								int item = Convert.ToInt32(value);
								list.Add(item);
							}
							catch (Exception)
							{
							}
						}
					}
				}
			}
			list.Sort();
			strName += "-";
			if (list.Count == 0)
			{
				return strName + "1";
			}
			return strName + (list[list.Count - 1] + 1).ToString();
		}

		private Stairs m_stair1;

		private Stairs m_stair2;

		private ExternalCommandData m_commandData;

		private Document m_doc;
	}
}
